package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.api.stock.AssistantToZgServiceAPI;
import com.steel.yfmall.cloud.assistant.bean.AssistantConstants;
import com.steel.yfmall.cloud.assistant.bean.ResponseZgMsgBean;
import com.steel.yfmall.cloud.assistant.bean.param.*;
import com.steel.yfmall.cloud.assistant.bean.param.zg.*;
import com.steel.yfmall.cloud.assistant.bean.result.APIResultAccountInfoBean;
import com.steel.yfmall.cloud.assistant.bean.result.APIResultByProductPriceBean;
import com.steel.yfmall.cloud.assistant.bean.result.ApiResultOrderAndInvoiceBean;
import com.steel.yfmall.cloud.assistant.dto.para.account.ParaAccountSyncAccount;
import com.steel.yfmall.cloud.assistant.dto.para.user.ParaUserCompanyBean;
import com.steel.yfmall.cloud.assistant.dto.para.view.ParaByProductPriceBean;
import com.steel.yfmall.cloud.assistant.dto.para.view.ParaOrderSettlementBean;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgCallWsdlAPIBaseDTO;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgSOResultBean;
import com.steel.yfmall.cloud.assistant.dto.zg.account.ZgResultAccountInfo;
import com.steel.yfmall.cloud.assistant.dto.zg.price.PriceEffectBean;
import com.steel.yfmall.cloud.assistant.dto.zg.result.ZgSO06Bean;
import com.steel.yfmall.cloud.assistant.dto.zg.sb.ResponseZgSystemUserAddressInfo;
import com.steel.yfmall.cloud.assistant.dto.zg.sb.ZgSystemUserAddressInfo;
import com.steel.yfmall.cloud.assistant.goods.ProductPriceFormService;
import com.steel.yfmall.cloud.assistant.oms.MasterOrderOmsAPIService;
import com.steel.yfmall.cloud.assistant.oms.mapper.MasterOrderAddressInfoMapper;
import com.steel.yfmall.cloud.assistant.oms.mapper.MasterOrderInfoMapper;
import com.steel.yfmall.cloud.assistant.oms.mapper.MasterOrderInvoiceMapper;
import com.steel.yfmall.cloud.assistant.po.oms.MasterOrderAddressInfo;
import com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInfo;
import com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInfoExample;
import com.steel.yfmall.cloud.assistant.po.oms.MasterOrderInvoice;
import com.steel.yfmall.cloud.assistant.po.user.CompanyShippingAddress;
import com.steel.yfmall.cloud.assistant.po.view.EndCodeBean;
import com.steel.yfmall.cloud.assistant.po.user.UserCompany;
import com.steel.yfmall.cloud.assistant.user.CompanyShippingAddressService;
import com.steel.yfmall.cloud.assistant.user.UserCompanyService;
import com.steel.yfmall.cloud.assistant.utils.constants.ZgCallAPIProperties;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgMethodBeanUtil;
import com.steel.yfmall.cloud.assistant.view.ByProductPriceBeanService;
import com.steel.yfmall.cloud.assistant.view.EndCodeBeanService;
import com.steel.yfmall.cloud.assistant.zg.ReceiveZGMessage;
import com.steel.yfmall.cloud.assistant.zg.ZgSBService;
import com.steel.yfmall.cloud.assistant.zg.ZgSOService;
import com.steel.yfmall.cloud.components.mq.bean.RequestSendMQBean;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import com.steel.yfmall.cloud.components.utils.base.APIListBean;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.components.utils.base.SOAStateBean;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import com.steel.yfmall.cloud.components.utils.utils.ResultCodeConstants;
import com.steel.yfmall.cloud.components.utils.utils.UtilDate;
import com.steel.yfmall.cloud.oms.api.bean.response.ResponseCreateMasterOrder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: PriceEffectBeanSericeAPIImpl
 * @Package com.steel.yfmall.cloud.assistant.biz.service.impl
 * @Description: (用一句话描述该文件做什么)
 * @date 2020/12/8 11:20
 */
@Slf4j
@Service
public class AssistantToZgServiceAPIImpl implements AssistantToZgServiceAPI {

    /**
     * @return com.steel.yfmall.cloud.components.utils.base.ResultZgMsgBean<java.lang.Boolean>    返回类型
     * @Title: batchTemplateProductPriceFactor
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param priceEffectBean 参数说明
     * @author yuchaohua
     * @date 2020/12/8 11:56
     */
    @Override
    public ResponseZgMsgBean<Boolean> priceEffect(APIPriceEffectBean apiParaBean) {
        ResponseZgMsgBean<Boolean> returnBean = new ResponseZgMsgBean<>();
        returnBean.setResult(false);
        // 校验毕传参数
        if (apiParaBean.getSr051vo() == null || StringUtils.isBlank(apiParaBean.getType())) {
            returnBean.setMsg(ResultCodeConstants.INVALID_MSG);
            return returnBean;
        }
        PriceEffectBean priceEffectBean = new PriceEffectBean();
        try {
            priceEffectBean = JSON.parseObject(JSON.toJSONString(apiParaBean), priceEffectBean.getClass());
        } catch (Exception e) {
            log.error("价格同步异常:" + JSONObject.toJSONString(apiParaBean), e);
            returnBean.setMsg("价格同步失败");
            return returnBean;
        }
        APIMsgBean<Boolean> batchTemplateProductPriceFactor = productPriceFormService.batchTemplateProductPriceFactor(priceEffectBean);
        if (batchTemplateProductPriceFactor == null) {
            returnBean.setMsg("价格同步失败");
            return returnBean;
        }
        returnBean.setResult(batchTemplateProductPriceFactor.getData());
        if (batchTemplateProductPriceFactor.getData()) {
            returnBean.setIsOk(ResultCodeConstants.SUCCESS);
            returnBean.setCode(ResultCodeConstants.SUCCESS);
        }
        if (batchTemplateProductPriceFactor.getSoaStateBean() != null) {
            returnBean.setMsg(batchTemplateProductPriceFactor.getSoaStateBean().getMessage());
        }
        return returnBean;
    }

    /**
     * @param apiParaBean
     * @return com.steel.yfmall.cloud.components.utils.base.APIListBean<com.steel.yfmall.cloud.assistant.biz.service.bean.result.APIResultAccountInfoBean>    返回类型
     * @Title: 查询余额信息
     * @Description: (这里用一句话描述这个方法的作用)
     * @date 2020/12/10 14:18
     * @author lang
     * @version 1.0
     */
    @Override
    public APIListBean<APIResultAccountInfoBean> getAccountInfo(APIParamAccountInfoBean apiParaBean) {
        APIListBean<APIResultAccountInfoBean> returnBean = new APIListBean<APIResultAccountInfoBean>();

        if (apiParaBean == null || StringUtils.isBlank(apiParaBean.getCompanyCode())) {
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("必传参数为空"));
            return returnBean;
        }

        try {
            ParaAccountSyncAccount paraAccountSyncAccount = new ParaAccountSyncAccount();
            paraAccountSyncAccount.setCustNo(apiParaBean.getCompanyCode());

            //封装接口参数
            //封装接口参数
            ZgCallWsdlAPIBaseDTO zgCallWsdlAPIBaseDTO = new ZgCallWsdlAPIBaseDTO();
            zgCallWsdlAPIBaseDTO.setUrl(zgCallAPIProperties.getSa_wskec_url());
            zgCallWsdlAPIBaseDTO.setInterfaceName(ZgMethodBeanUtil.GET_ACCOUNT_DATA_INTERFACE);
            zgCallWsdlAPIBaseDTO.setParameterType(ZgMethodBeanUtil.SA_SYSTEM);
            ZgSOResultBean zgSOResultBean = zgSOService.getAccountDate(paraAccountSyncAccount, zgCallWsdlAPIBaseDTO);
            if (zgSOResultBean != null && StringUtils.isNotBlank(zgSOResultBean.getIsSuccess()) && StringUtils.equalsIgnoreCase(zgSOResultBean.getIsSuccess(), "Y")) {
                ZgResultAccountInfo zgResultAccountInfo = new ZgResultAccountInfo();
                if (StringUtils.isNotBlank(zgSOResultBean.getData())) {
                    List<ZgResultAccountInfo.DATABean> accountDataInfo = JSONObject.parseArray(JSON.toJSONString(JSON.parse(zgSOResultBean.getData().toString())), ZgResultAccountInfo.DATABean.class);
                    zgResultAccountInfo.setDATA(accountDataInfo);
                }

                //循环封装返回参数
                List<APIResultAccountInfoBean> returnlist = new ArrayList<>();
                if (zgResultAccountInfo != null && zgResultAccountInfo.getDATA() != null && zgResultAccountInfo.getDATA().size() > 0) {
                    for (ZgResultAccountInfo.DATABean dataBean : zgResultAccountInfo.getDATA()) {
                        APIResultAccountInfoBean apiResultAccountInfoBean = new APIResultAccountInfoBean();
                        MobileBaseBeanUtils.copyAllFields(dataBean, apiResultAccountInfoBean);
                        returnlist.add(apiResultAccountInfoBean);
                    }
                }

                returnBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean("查询账户信息成功"));
                returnBean.setData(returnlist);
            } else {
                returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("查询账户信息失败"));
            }
        } catch (Exception e) {
            log.error("查询中冠账户余额信息失败,查询账户为：" + JSON.toJSONString(apiParaBean), e);
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("查询账户信息异常"));
            return returnBean;
        }
        return returnBean;
    }

    /**
     * 同步企业收货地址给中冠
     *
     * @param apiParaBean 参数说明
     * @return com.steel.yfmall.cloud.components.utils.base.APIMsgBean<java.lang.Boolean>    返回类型
     * @Title: templateCompanyAddress
     * @Description: (这里用一句话描述这个方法的作用)
     * @author yuchaohua
     * @date 2020/12/10 22:31
     */
    @Override
    public APIMsgBean<Boolean> templateCompanyAddress(APIParamCompanyAddressBean apiParaBean) {
        APIMsgBean<Boolean> returnBean = new APIMsgBean<>();
        returnBean.setData(false);
        try {
            Map<String, String> checkParam = new HashMap<>();
            checkParam.put("addressId", "需要同步的地址ID");
            SOAStateBean soaStateBean = ResponseCode.checkApiParameter(checkParam, apiParaBean);
            if (soaStateBean == null || StringUtils.isNotBlank(soaStateBean.getMessage())) {
                returnBean.setSoaStateBean(soaStateBean);
                return returnBean;
            }
            // 同步用户收货地址
            CompanyShippingAddress resultAddress = companyShippingAddressService.getAddressById(apiParaBean.getAddressId());
            if (null == resultAddress) {
                returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("收货人地址不存在为空"));
                return returnBean;
            }
            if (StringUtils.isBlank(resultAddress.getCompanyCode())) {
                returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("收货人地址公司信息为空"));
                return returnBean;
            }
            ParaUserCompanyBean paraBean=new ParaUserCompanyBean();
            paraBean.setCompanyCode(resultAddress.getCompanyCode());
            UserCompany userCompany=userCompanyService.getUserCompanyByPara(paraBean);
            if (null == userCompany) {
                returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("收货人公司信息无效!"));
                return returnBean;
            }
            // 地址调整动作标识  必填，N:新增、R：修改、D:删除
            String inputCode = "N";
            if (StringUtils.isNotBlank(resultAddress.getZgRecOrderNo())) {
                inputCode = "R";
            }
            ZgSystemUserAddressInfo resultUserCompanySale = new ZgSystemUserAddressInfo();
            resultUserCompanySale.setInputCode(inputCode);
            resultUserCompanySale.setPhone(resultAddress.getMobile());
            resultUserCompanySale.setCustNo(resultAddress.getCompanyCode());
            resultUserCompanySale.setSiteNo(resultAddress.getLocaleCode());
            resultUserCompanySale.setAreaNo(resultAddress.getFinishAreaCode());
            resultUserCompanySale.setProvince(resultAddress.getProvinceCode());
            resultUserCompanySale.setCity(resultAddress.getCityCode());
            resultUserCompanySale.setConty(resultAddress.getDistrictCode());
            resultUserCompanySale.setRecAddr(resultAddress.getAddress());
            // 邮编是终到地点 减掉2位
            if (StringUtils.isNotBlank(resultAddress.getLocaleCode())) {
                resultUserCompanySale.setZipCode(StringUtils.left(resultAddress.getLocaleCode(), resultAddress.getLocaleCode().length() - 2));
            }
            resultUserCompanySale.setRecName(userCompany.getCompanyName());
            resultUserCompanySale.setRecShortName(userCompany.getCompanyName());
            resultUserCompanySale.setContactMan(resultAddress.getConsignee());
            resultUserCompanySale.setRecOrderNo(resultAddress.getZgRecOrderNo());
            resultUserCompanySale.setMemo("收货人同步");
            resultUserCompanySale.setStatus("Y");
            APIMsgBean<ResponseZgSystemUserAddressInfo> sendZgAddress = zgSBService.synchZgSystemUSerAddressInfo(resultUserCompanySale);
            if (sendZgAddress.getData() != null) {
                if (StringUtils.equalsIgnoreCase(inputCode, "N")) {
                    CompanyShippingAddress recordBean = new CompanyShippingAddress();
                    recordBean.setAddressId(resultAddress.getAddressId());
                    recordBean.setZgRecOrderNo(sendZgAddress.getData().getRecorderNo());
                    companyShippingAddressService.updateCompanyShippingAddressById(recordBean);
                }
                returnBean.setData(true);
            }
            returnBean.setSoaStateBean(sendZgAddress.getSoaStateBean());
        } catch (Exception e) {
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("收货人信息调整推送中冠系统信息异常"));
            log.error("收货人信息调整推送中冠系统信息异常 : " + JSONObject.toJSONString(apiParaBean), e);
        }
        return returnBean;
    }

    /**
     * 结算后抛送订单与装车单
     *
     * @param apiParaBean 订单号	orderNo
     *                    装车单号	invoiceNo
     *                    结算金额	goodsTaiAmt
     */
    @Override
    public void doOrderSettlementInfo(List<APIParamOrderAndInvoiceBean> apiParaBean) {
        if (apiParaBean == null || apiParaBean.size() == 0) {
            return;
        }

        for (APIParamOrderAndInvoiceBean bean : apiParaBean) {
            //配置返回值
            ApiResultOrderAndInvoiceBean apiResultOrderAndInvoiceBean = new ApiResultOrderAndInvoiceBean();

            MasterOrderInfoExample orderInfoExample = new MasterOrderInfoExample();
            String invoiceNos = bean.getInvoiceNo();
            List<String> invoiceNo = Arrays.asList(invoiceNos.split(","));
            orderInfoExample.createCriteria().andMasterOrderSnEqualTo(bean.getOrderNo()).andCustomerContractNumIn(invoiceNo);
            MasterOrderInfo masterOrderInfo = masterOrderInfoMapper.selectOneByExampleSelective(orderInfoExample);

            MasterOrderAddressInfo masterOrderAddressInfo = masterOrderAddressInfoMapper.selectByPrimaryKey(bean.getOrderNo());

//            MasterOrderGoodsExample orderGoodsExample = new MasterOrderGoodsExample();
//            orderGoodsExample.createCriteria().andMasterOrderSnEqualTo(bean.getOrderNo());
//            MasterOrderGoods masterOrderGoods = masterOrderGoodsMapper.selectOneByExampleSelective(orderGoodsExample);

            //订单号
            apiResultOrderAndInvoiceBean.setOrderSn(bean.getOrderNo());
            //出库时间
            apiResultOrderAndInvoiceBean.setShipDate(UtilDate.getDate());
            //商品编码 不需要
//            apiResultOrderAndInvoiceBean.setCustomCode(masterOrderGoods.getCustomCode());
            //快递单号(多个逗号分隔)
            apiResultOrderAndInvoiceBean.setInvoiceNo(bean.getInvoiceNo());
            //结算总金额
            apiResultOrderAndInvoiceBean.setGoodsTaiAmt(bean.getGoodsTaiAmt());
            //交货单号 不需要
//            apiResultOrderAndInvoiceBean.setShipSn();
            //快递编号
            apiResultOrderAndInvoiceBean.setShippingCode(masterOrderAddressInfo.getShippingCode());
            //快递名称
            apiResultOrderAndInvoiceBean.setShippingName(masterOrderAddressInfo.getShippingName());
            //发送MQ
            RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
            requestSendMQBean.setSendMqTitle("strade_order_notice_ship");
            requestSendMQBean.setSendMqMessage(JSONObject.toJSONString(apiResultOrderAndInvoiceBean));
            mqSendService.sendCommonMQMessage(requestSendMQBean);
        }

        StringBuilder sb = new StringBuilder("中冠结算成功，抛送：");
        for (APIParamOrderAndInvoiceBean apiParamOrderAndInvoiceBean : apiParaBean) {
            sb.append("订单【")
                    .append(apiParamOrderAndInvoiceBean.getOrderNo())
                    .append("】与装车单【")
                    .append(apiParamOrderAndInvoiceBean.getInvoiceNo())
                    .append("】, ");
        }
        log.info(sb.toString());
    }

    /**
     * 开立发票后向电商反馈
     *
     * @param apiParaBean 订单号		orderNo
     *                    电商号		draftNo
     *                    申请开票单号	avNo
     */
    @Override
    public ResponseZgMsgBean<String> invoiceResult(APIParamInvoiceResultBean apiParaBean) {
        ResponseZgMsgBean<String> responseZgMsg = new ResponseZgMsgBean<>();
        String orderNo = apiParaBean.getOrderNo();
        String avNo = apiParaBean.getAvNo();
        String draftNo = apiParaBean.getDraftNo();
        //参数校验
        try {
            //订单号校验
            MasterOrderInfo masterOrderInfo = masterOrderInfoMapper.selectByPrimaryKey(draftNo);
            if (StringUtils.isBlank(draftNo) || null == masterOrderInfo || 2 == masterOrderInfo.getOrderStatus()) {
                log.error("订单【{}】不存在或不合法", JSON.toJSONString(apiParaBean));
                throw new NullPointerException("订单不存在或不合法：" + draftNo);
            }
            //合同号校验
            String outContractSn = masterOrderInfo.getOutContractSn();
            if (!StringUtils.equalsIgnoreCase(orderNo, outContractSn)) {
                log.error("订单【{}】中所提供合同号【{}】与实际合同号【{}】不符", draftNo, orderNo, outContractSn);
                throw new NullPointerException("提供订单号【" + orderNo + "】中的电商合同号【" + orderNo + "】与实际【" + outContractSn + "】不符");
            }
            //申请开票单号校验
            MasterOrderInvoice masterOrderInvoice = masterOrderInvoiceMapper.selectByPrimaryKey(draftNo);
            if (StringUtils.isBlank(avNo) || null == masterOrderInvoice || !StringUtils.equalsIgnoreCase(avNo, masterOrderInvoice.getOtherInvoiceOrderSn())) {
                log.error("申请开票单号【{}】与实际不符或不存在或不合法", avNo);
                throw new NullPointerException("申请开票单号【" + avNo + "】与实际不符或不存在或不合法");
            }
        } catch (NullPointerException e) {
            responseZgMsg.setCode(0);
            responseZgMsg.setMsg(e.getMessage());
            return responseZgMsg;
        }

        MasterOrderInvoice masterOrderInvoice = new MasterOrderInvoice();
        masterOrderInvoice.setMasterOrderSn(draftNo);
        masterOrderInvoice.setUpdateTime(new Date());
        //修改为已开具发票
        masterOrderInvoice.setHandleType(1);
        masterOrderInvoiceMapper.updateByPrimaryKeySelective(masterOrderInvoice);
        responseZgMsg.setCode(1);
        responseZgMsg.setMsg("success！");
        return responseZgMsg;
    }

    /**
     * 在订单建立之后中冠会通知电商，订单状态变更信息通知。
     *
     * @param apiParaBean 传送代号	Formid
     *                    电商订单编号	draftOrderNo
     *                    产销订单号	orderNo
     *                    产销订单状态	status
     */
    @Override
    public ResponseZgMsgBean<String> productionStatusChange(APIParamProductionStatusChangeBean apiParaBean) {
        ResponseZgMsgBean<String> resultMsg = new ResponseZgMsgBean<>();
        String draftOrderNo = apiParaBean.getDraftOrderNo();
        String status = apiParaBean.getStatus();
        String orderNo = apiParaBean.getOrderNo();
        MasterOrderInfo masterOrderInfo = masterOrderInfoMapper.selectByPrimaryKey(draftOrderNo);
        try {
            //参数校验
            if (StringUtils.isBlank(draftOrderNo)) {
                throw new NullPointerException("订单编号为空");
            }
            if (masterOrderInfo == null) {
                throw new NullPointerException("订单" + draftOrderNo + "不存在");
            }
            if (StringUtils.isBlank(status)) {
                throw new NullPointerException("订单编号为空");
            }
            if (StringUtils.isBlank(orderNo)) {
                throw new NullPointerException("订单编号为空");
            }
        } catch (NullPointerException e) {
            resultMsg.setMsg(e.getMessage());
            return resultMsg;
        }
        masterOrderInfo.setOutOrderStatus(status);
        masterOrderInfoMapper.updateByPrimaryKeySelective(masterOrderInfo);
        if (StringUtils.equalsIgnoreCase(masterOrderInfo.getOutContractSn(), orderNo)) {
            log.info("流程成功！");
            resultMsg.setIsSuccess("Y");
            resultMsg.setCode(1);
        } else {
//            resultMsg.setMsg("合同号与订单号不符");
            log.warn("合同号【{}】与订单号【{}]不匹配", orderNo, masterOrderInfo.getOutContractSn());
            resultMsg.setIsSuccess("N");
            resultMsg.setCode(0);
        }
        return resultMsg;
    }

    /**
     * 产销框架合同变动时，通知电商，这里修改电商的框架合同状态
     *
     * @param apiZgSO06Bean
     * @return 结果
     * @author jia chang bao
     */
    @Override
    public ResponseZgMsgBean<String> updateFrameworkContractInfo(APIZgSO06Bean apiZgSO06Bean) {
        ResponseZgMsgBean<String> responseZgMsgBean = new ResponseZgMsgBean<>();
        ZgSO06Bean zgSO06Bean = new ZgSO06Bean();
        MobileBaseBeanUtils.copyAllFields(apiZgSO06Bean, zgSO06Bean);
        APIMsgBean<Boolean> apiMsgBean = receiveZGMessage.ReceiveZGSo06Message(zgSO06Bean);
        if (apiMsgBean.getData()) {
            responseZgMsgBean.setIsOk(1);
            responseZgMsgBean.setCode(1);
            responseZgMsgBean.setMsg("成功");
            responseZgMsgBean.setIsSuccess("Y");
            responseZgMsgBean.setData("");
            return responseZgMsgBean;
        }
        responseZgMsgBean.setIsOk(0);
        responseZgMsgBean.setCode(0);
        responseZgMsgBean.setMsg("失败");
        responseZgMsgBean.setIsSuccess("N");
        responseZgMsgBean.setData("");
        return responseZgMsgBean;
    }

    /**
     * 根据 订单号 和 客户编号 查询结算单信息
     *
     * @param apiParamOrderSettlementBean
     * @return 结果
     * @author jia chang bao
     */
    @Override
    public APIListBean<APIParamOrderSettlementBean> getOrderSettlement(APIParamOrderSettlementBean apiParamOrderSettlementBean) {
        APIListBean<APIParamOrderSettlementBean> returnBean = new APIListBean<>();
        Map<String, String> checkParam = new HashMap<>();
        checkParam.put("orderNo", "合同号");
        checkParam.put("custNo", "客户编号");
        SOAStateBean soaStateBean = ResponseCode.checkApiParameter(checkParam, apiParamOrderSettlementBean);
        if (soaStateBean == null || StringUtils.isNotBlank(soaStateBean.getMessage())) {
            returnBean.setData(null);
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean(soaStateBean.getMessage()));
            return returnBean;
        }
        try {
            //设置订单号和客户编号
            ParaOrderSettlementBean paraAccountSyncAccount = new ParaOrderSettlementBean();
            paraAccountSyncAccount.setCustNo(apiParamOrderSettlementBean.getCustNo());
            paraAccountSyncAccount.setOrderNo(apiParamOrderSettlementBean.getOrderNo());

            //封装接口参数
            ZgCallWsdlAPIBaseDTO zgCallWsdlAPIBaseDTO = new ZgCallWsdlAPIBaseDTO();
            zgCallWsdlAPIBaseDTO.setUrl(zgCallAPIProperties.getSa_wskec_url());
            zgCallWsdlAPIBaseDTO.setInterfaceName(ZgMethodBeanUtil.GET_ORDER_SETTLEMENT);
            zgCallWsdlAPIBaseDTO.setParameterType(ZgMethodBeanUtil.SA_SYSTEM);
            //请求中冠接口数据
            ZgSOResultBean orderSettlement = zgSOService.getOrderSettlement(paraAccountSyncAccount, zgCallWsdlAPIBaseDTO);
            if (orderSettlement != null && StringUtils.isNotBlank(orderSettlement.getIsSuccess()) && StringUtils.equalsIgnoreCase(orderSettlement.getIsSuccess(), "Y")) {
                String data = orderSettlement.getData();
                List<APIParamOrderSettlementBean> apiParamOrderSettlementBeans = JSONObject.parseArray(data, APIParamOrderSettlementBean.class);

                returnBean.setData(apiParamOrderSettlementBeans);
                return returnBean;
            } else {
                returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("查询账户信息失败"));
            }
        } catch (Exception e) {
            log.error("查询中冠结算信息失败：" + JSON.toJSONString(apiParamOrderSettlementBean), e);
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("查询账户信息异常"));
            return returnBean;
        }
        return returnBean;
    }

    /**
     * 中冠订单变更
     *
     * @return com.steel.yfmall.cloud.assistant.bean.ResponseZgMsgBean<java.lang.String>    返回类型
     * @Title: productionOrderChange
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param apiParaBean 参数说明
     * @author yuchaohua
     * @date 2021/1/14 18:28
     */
    @Override
    public ResponseZgMsgBean<String> productionOrderChange(APIZgChangerOrderBean apiParaBean) {
        ResponseZgMsgBean<String> resultMsg = new ResponseZgMsgBean<>();
        if (apiParaBean == null || StringUtils.isBlank(apiParaBean.getOrderNo()) || null == apiParaBean.getMain() || CollectionUtils.isEmpty(apiParaBean.getDetails())) {
            resultMsg.setMsg("必传参数为空");
            return resultMsg;
        }
        try {
            APIMsgBean<ResponseCreateMasterOrder> changeOrderFoZg = masterOrderOmsAPIService.createChangeOrderFoZg(apiParaBean);
            if (changeOrderFoZg == null || changeOrderFoZg.getData() == null) {
                resultMsg.setMsg("订单变更失败");
                if (changeOrderFoZg != null && changeOrderFoZg.getSoaStateBean() != null) {
                    resultMsg.setMsg(changeOrderFoZg.getSoaStateBean().getMessage());
                }
                return resultMsg;
            } else {
                resultMsg.setResult(changeOrderFoZg.getData().getMasterOrderSn());
            }
        } catch (Exception e) {
            log.error("订单变更失败", e);
            resultMsg.setMsg("订单变更失败");
            return resultMsg;
        }
        resultMsg.setIsOk(ResultCodeConstants.SUCCESS);
        resultMsg.setCode(ResultCodeConstants.SUCCESS);
        resultMsg.setIsSuccess("Y");
        resultMsg.setMsg("订单变更成功");
        return resultMsg;
    }

    /**
     * 查询副产品价格
     *
     * @return com.steel.yfmall.cloud.components.utils.base.APIMsgBean<com.steel.yfmall.cloud.assistant.bean.result.APIResultByProductPriceBean>    返回类型
     * @Title: getByProductPriceBeanByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param apiParaBean 参数说明
     * @author yuchaohua
     * @date 2021/2/23 12:25
     */
    @Override
    public APIMsgBean<APIResultByProductPriceBean> getByProductPriceBeanByPara(APIParamByProductPriceBean apiParaBean) {
        APIMsgBean<APIResultByProductPriceBean> returnBean = new APIMsgBean<>();
        Map<String, String> checkParam = new HashMap<>();
        checkParam.put("prodType", "产品形态");
        checkParam.put("custNo", "客户编号");
        SOAStateBean soaStateBean = ResponseCode.checkApiParameter(checkParam, apiParaBean);
        if (soaStateBean == null || StringUtils.isNotBlank(soaStateBean.getMessage())) {
            returnBean.setData(null);
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean(soaStateBean.getMessage()));
            return returnBean;
        }
        try {
            //设置订单号和客户编号
            ParaByProductPriceBean paraBean = new ParaByProductPriceBean();
            paraBean.setCustNo(apiParaBean.getCustNo());
            paraBean.setProdType(apiParaBean.getProdType());
            paraBean.setPriceEffectTime(UtilDate.getDate());
            paraBean.setStatus("Y");
            APIResultByProductPriceBean priceBean = byProductPriceBeanService.getByProductPriceBeanByPara(paraBean);
            if (priceBean != null) {
                returnBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
                returnBean.setData(priceBean);
            } else {
                returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("未查询到相关有效副产品价格信息"));
            }
        } catch (Exception e) {
            log.error("查询到相关有效副产品价格信息失败：" + JSON.toJSONString(apiParaBean), e);
            returnBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("未查询到相关有效副产品价格信息"));
            return returnBean;
        }
        return returnBean;
    }

    /**
     * 接收中冠终到地点数据
     *
     * @param apiParamEndCodeBean
     * @return 结果
     * @author jia chang bao
     */
    @Override
    public APIMsgBean<Boolean> receiveZgEndCode(APIParamEndCodeBean apiParamEndCodeBean) {
        log.info("中冠的终到地点数据发生变化，同步到电商：" + JSONObject.toJSONString(apiParamEndCodeBean));
        APIMsgBean<Boolean> apiMsgBean = new APIMsgBean<>();
        apiMsgBean.setData(false);
        apiMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("同步到电商失败"));
        if (AssistantConstants.ZG_ENDCODE_UPDATA.equals(apiParamEndCodeBean.getAction())) {
            //修改
            Boolean update = false;
            if (!CollectionUtils.isEmpty(apiParamEndCodeBean.getData())) {
                for (APIParamEndCodeBean apiParamEndCode : apiParamEndCodeBean.getData()) {
                    EndCodeBean endCodeBean = new EndCodeBean();
                    MobileBaseBeanUtils.copyAllFields(apiParamEndCode, endCodeBean);
                    int i = endCodeBeanService.updateEndCode(endCodeBean);
                    log.info("本次修改"+i);
                    if (i > 0) {
                        update = true;
                    } else {
                        update = false;
                    }
                }
            }
            if (update) {
                apiMsgBean.setData(true);
                apiMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("同步到电商成功"));
                return apiMsgBean;
            }
        }
        if (AssistantConstants.ZG_ENDCODE_DELETE.equals(apiParamEndCodeBean.getAction())) {
            //删除
            Boolean delete = false;
            if (!CollectionUtils.isEmpty(apiParamEndCodeBean.getData())) {
                for (APIParamEndCodeBean apiParamEndCode : apiParamEndCodeBean.getData()) {
                    EndCodeBean endCodeBean = new EndCodeBean();
                    endCodeBean.setStatus(0);
                    MobileBaseBeanUtils.copyAllFields(apiParamEndCode, endCodeBean);
                    int i = endCodeBeanService.updateEndCode(endCodeBean);
                    log.info("本次修改删除状态"+i);
                    if (i > 0) {
                        delete = true;
                    } else {
                        delete = false;
                    }
                }
            }
            if (delete) {
                apiMsgBean.setData(true);
                apiMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("同步到电商成功"));
                return apiMsgBean;
            }
        }
        if (AssistantConstants.ZG_ENDCODE_CREATE.equals(apiParamEndCodeBean.getAction())) {
            //创建
            Boolean create = false;
            if (!CollectionUtils.isEmpty(apiParamEndCodeBean.getData())) {
                for (APIParamEndCodeBean apiParamEndCode : apiParamEndCodeBean.getData()) {
                    EndCodeBean endCodeBean = new EndCodeBean();
                    endCodeBean.setStatus(1);
                    endCodeBean.setCreateTime(new Date());
                    MobileBaseBeanUtils.copyAllFields(apiParamEndCode, endCodeBean);
                    int i = endCodeBeanService.addEndCode(endCodeBean);
                    log.info("本次创建"+i);
                    if (i > 0) {
                        create = true;
                    } else {
                        create = false;
                    }
                }
            }
            if (create) {
                apiMsgBean.setData(true);
                apiMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("同步到电商成功"));
                return apiMsgBean;
            }
        }
        return apiMsgBean;
    }

    @Resource
    private MasterOrderOmsAPIService masterOrderOmsAPIService;

    /**
     * 终到地点
     */
    @Resource
    private EndCodeBeanService endCodeBeanService;
    /**
     * 价格服务
     */
    @Resource
    private ProductPriceFormService productPriceFormService;

    @Resource
    private ZgSOService zgSOService;

    @Resource
    private MqSendService mqSendService;

    @Resource
    private MasterOrderInfoMapper masterOrderInfoMapper;

    @Resource
    private MasterOrderAddressInfoMapper masterOrderAddressInfoMapper;

    @Resource
    private ZgCallAPIProperties zgCallAPIProperties;

    @Resource
    private ZgSBService zgSBService;

    @Resource
    private MasterOrderInvoiceMapper masterOrderInvoiceMapper;

    /**
     * 地址信息
     */
    @Resource
    private CompanyShippingAddressService companyShippingAddressService;

    /**
     * 接收中冠 回传 数据
     */
    @Resource
    private ReceiveZGMessage receiveZGMessage;

    @Resource
    private ByProductPriceBeanService byProductPriceBeanService;

    @Resource
    private UserCompanyService userCompanyService;

}
